package com.neuedu.shop.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.alibaba.fastjson.JSON;
import com.neuedu.shop.mapper.CategoryMapper;
import com.neuedu.shop.pojo.Category;
import com.neuedu.shop.service.CategoryService;
import com.neuedu.shop.service.impl.CategoryServiceImpl;
import com.neuedu.shop.util.MybatisUtil;
import com.neuedu.shop.util.ServletUtil;



/**
 * @author MrDCG
 * @version 创建时间：2019年9月22日 下午3:33:08
 *
 */
@WebServlet("*.category")
public class CategoryServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7888089347449321517L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取前台的url地址
		String url = request.getRequestURL().toString();
		// 将url地址截取出需要的字段 /findAll
		String path = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
		// 设置utf-8字符集，解决中文乱码问题
		ServletUtil.getCharacterEncoding(request, response);
		// 获取session对象
		SqlSession sqlSession = MybatisUtil.getSession("mybatis-config.xml");
		PrintWriter out = response.getWriter();
		// 通过反射拿到mapper对象
		CategoryMapper cateMapper = sqlSession.getMapper(CategoryMapper.class);
		CategoryService cateService = new CategoryServiceImpl(cateMapper);
		if ("/findToTree".equals(path)) {
			List<Category> cates = cateService.findToTree();
			out.print(JSON.toJSONString(cates));
//			request.setAttribute("cates", cates);
//			request.getRequestDispatcher("category.jsp").forward(request, response);
		} else if("/findAll".equals(path)) {
			List<Category> cates = cateService.findAll();
			request.setAttribute("cates", cates);
			request.getRequestDispatcher("category.jsp").forward(request, response);
		} else if("/child".equals(path)) {
			int parentId = Integer.parseInt(request.getParameter("id"));
			String categoryName = request.getParameter("categoryName");
			request.setAttribute("parentId", parentId);
			request.setAttribute("categoryName", categoryName);
			request.getRequestDispatcher("insertChild.jsp").forward(request, response);
		} else if("/insertChild".equals(path)) {
			int parentId = Integer.parseInt(request.getParameter("parentId"));
			String catename = request.getParameter("catename");
			String catedesc = request.getParameter("catedesc");
			cateService.insertChild(catename, catedesc, parentId);
			sqlSession.commit();
			response.sendRedirect("findAll.category");
		} else if("/insertRoot".equals(path)) {
			String categoryName = request.getParameter("catename");
			String categoryDescription = request.getParameter("catedescr");
			cateService.insertRoot(categoryName, categoryDescription);
			sqlSession.commit();
			response.sendRedirect("findAll.category");
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}
}
